/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF WIKIX.
WIKIX IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

WIKIX IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH WIKIX. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using EPiServer.Core;
using EPiServer.PlugIn;

namespace EPiServer.Research.Wiki.Properties
{
    /// <summary>
    /// Custom PropertyData implementation
    /// </summary>
    [Serializable]
    [PageDefinitionTypePlugIn]
    public class PropertyAccessLevel : EPiServer.Core.PropertyString
    {

        /// <summary>
        /// Returns true if the current user is allowed according to this setting.
        /// </summary>
        /// <returns></returns>
        public bool CheckAllowed(PageData pd)
        {
            if (IsLoggedIn) return (!EPiServer.Personalization.EPiServerProfile.Current.IsAnonymous);
            return pd.QueryDistinctAccess(this.AccessLevel);
        }

        public bool IsLoggedIn
        {
            get
            {
                return (String.ToLower() == "logged in");
            }
        }

        public EPiServer.Security.AccessLevel AccessLevel
        {
            get
            {
                if (IsLoggedIn) return EPiServer.Security.AccessLevel.Undefined;
                return (EPiServer.Security.AccessLevel)Enum.Parse(typeof(EPiServer.Security.AccessLevel), String);
            }
            set
            {
                String = value.ToString();
            }
        }

        public override EPiServer.Core.IPropertyControl CreatePropertyControl()
        {
            return new PropertyAccessLevelControl();
        }

    }
}
